package com.stylefeng.guns.modular.flowerType.controller;

import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.FlowerTypeMapper;
import com.stylefeng.guns.common.persistence.model.FlowerType;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.flowerType.service.IFlowerTypeService;
import com.stylefeng.guns.modular.system.warpper.FlowerTypeWarpper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * flowerType控制器
 *
 * @author fengshuonan
 * @Date 2019-07-11 17:38:30
 */
@Controller
@RequestMapping("/flowerType")
public class FlowerTypeController extends BaseController {

    private String PREFIX = "/flowerType/flowerType/";

    @Resource
    private IFlowerTypeService flowerTypeService;

    @Resource
    private FlowerTypeMapper flowerTypeMapper;

    /**
     * 跳转到flowerType首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "flowerType.html";
    }

    /**
     * 跳转到添加flowerType
     */
    @RequestMapping("/flowerType_add")
    public String flowerTypeAdd() {
        return PREFIX + "flowerType_add.html";
    }

    /**
     * 跳转到修改flowerType
     */
    @RequestMapping("/flowerType_update/{flowersId}")
    public String flowerTypeUpdate(@PathVariable Integer flowersId, Model model) {
        FlowerType flowerType = flowerTypeService.selectFlowerTypeById(flowersId);
        model.addAttribute(flowerType);
        return PREFIX + "flowerType_edit.html";
    }

    /**
     * 获取flowerType列表
     */
    @ApiOperation("查看花卉类型列表")
    @PostMapping(value = "/list")
    @ResponseBody
    public Object list(@ModelAttribute String condition) {
        List<Map<String, Object>> list = flowerTypeService.showFlowerTypeTable(condition);
        return super.warpObject(new FlowerTypeWarpper(list));
    }

    /**
     * 新增flowerType
     */
    @ApiOperation("添加花卉类型")
    @PostMapping(value = "/add")
    @ResponseBody
    public Object add(@ModelAttribute FlowerType flowerType) {
        flowerTypeMapper.insert(flowerType);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除flowerType
     */
    @ApiOperation("删除类型信息")
    @PostMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer flowersId) {
        flowerTypeService.delFlowerType(flowersId);
        return SUCCESS_TIP;
    }

    /**
     * 修改flowerType
     */
    @ApiOperation("修改类型信息")
    @PostMapping(value = "/update")
    @ResponseBody
    public Object update(@ModelAttribute FlowerType flowerType) {

        if (ToolUtil.isEmpty(flowerType) || flowerType.getFid() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        flowerTypeService.updateFlowerType(flowerType);
        return super.SUCCESS_TIP;
    }

    /**
     * flowerType详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
